//
//  LoginRequestAPI.swift
//  XGamesLive
//
//  Created by ios on 2024/5/14.
//

import Foundation
import PanKit
import Moya
import SwiftyRSA

class LoginRequestAPI: PanRequestBaseAPI,XGL_RequestConfigurationProtocol {
    enum LoginAPI{
        case LoginWithApple(thirdId: String,appleCode: String,nickname: String?)
        case LoginWithPassword(password: String,email: String)
        case RegisterWithEmail(email: String,password: String)
        case VerificationEmail(email: String)
        case CompleteInfor(infor: LoginData,param1: String,param2: String)
    }
    
    init(requestAPI: LoginAPI) {
        super.init()
        self.resuestAPI = requestAPI
        self.requestConfiguration()
    }
    private var resuestAPI: LoginAPI!
    
    override var path: String{
        switch self.resuestAPI {
        case .LoginWithApple:
            return "/BerryApi/users/getUser"
        case .VerificationEmail:
            return "/BerryApi/users/checkEmailCanBeRegisted"
        case .RegisterWithEmail:
            return "/BerryApi/users/registUserByEmail"
        case .LoginWithPassword:
            return "/BerryApi/users/loginUserByEmail"
        case .CompleteInfor(_,let param1,let param2):
            return "/BerryApi/\(param1)/addOrEdit\(param2)UserExt"
        default:
            return ""
        }
    }
    
    override var method: Moya.Method{
        switch self.resuestAPI {
        case .VerificationEmail,.RegisterWithEmail,.LoginWithPassword,.CompleteInfor,.LoginWithApple:
            return .post
        default:
            return .get
        }
    }
    
    override var task: Task{
        switch self.resuestAPI {
        case .VerificationEmail,.RegisterWithEmail,.LoginWithPassword,.CompleteInfor,.LoginWithApple:

            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        default:
//            if parameters.isEmpty == false{
//                let parameStr = parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
//                let rsaParams = XGL_PRSA.encryptString(parameStr, publicKey: XGL_EncipherTool.share.cpk)
//                return .requestParameters(parameters: ["encryptParam": rsaParams ?? ""],encoding: parameterEncoding)
//            }
            if parameters.isEmpty == false{
                let parameStr = parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
                do {
                    let publicKey = try PublicKey(base64Encoded: XGL_EncipherTool.share.cpk)
                    let encryptedStr = try ClearMessage(string: parameStr, using: .utf8).encrypted(with: publicKey, padding: .PKCS1)
                    return .requestParameters(parameters: ["encryptParam": encryptedStr],encoding: parameterEncoding)
                } catch {
                print(error)
                }
            }
            return .requestParameters(parameters: parameters,encoding: parameterEncoding)
        }
    }
    
    override var cacheModel: PanRequestBaseAPI.PanRequestCacheModel{
        return .none
    }
    
    override var cacheKey: String{
        return "test"
    }
    
    override var parameters: [String : Any]{
        var dic = super.parameters
        switch self.resuestAPI {
        case .LoginWithApple(let thirdId,let appleCode,let nickname):
            dic["thirdId"] = thirdId
            dic["appleCode"] = appleCode
            if let name = nickname {
                dic["nickname"] = name
            }
            return dic
        case .VerificationEmail(let email):
            dic["email"] = email
            return dic
        case .RegisterWithEmail(let email,let password):
            dic["email"] = email
            dic["password"] = password
            return dic
        case .LoginWithPassword(let password,let email):
            dic["email"] = email
            dic["password"] = password
            return dic
        case .CompleteInfor(let infor,_,_):
            dic["ignoreEditNullParam"] = 1
            dic["pid"] = 0
            if let id = infor.treeId {
                dic["id"] = id
            }
            dic["data1"] = infor.nickname
            dic["data2"] = infor.header
            dic["data3"] = infor.gender ?? 0
            dic["data4"] = infor.games.joined(separator: ",")
            return dic
        default:
            return dic
        }
    }
}

